package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.NoticeBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.logUtilssss;
import com.tybbank.web.WebActivity;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/27 0027.
 */

public class NoticeActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rl_RecyclerView_list)
    RecyclerView rv_recyclerView;
    @BindView(R.id.ptr_scroll_view)
    PullToRefreshScrollView ptrScrollView;
    @BindView(R.id.rl_gone)
    RelativeLayout rlGone;
    private int page = 1;
    private List<NoticeBean.DataBean>  list = new ArrayList<>();
    private String type="";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        ButterKnife.bind(this);
        toolbar(toolbar, getIntent().getStringExtra("title"), R.mipmap.icon_close);
        //信息类型1app 首页新闻 2 公告 3 帮助
        type = getIntent().getStringExtra("type");
        page = 1;
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv_recyclerView.setLayoutManager(manager);
        rv_recyclerView.setHasFixedSize(true);//每个 高度一样
        //创建并设置adapter
        rv_recyclerView.setNestedScrollingEnabled(false);
        ptrScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page = 1;
                init();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                init();
            }
        });
        init();
    }

    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("page", page + "");
        parameters.put("TimeStamp", time);
        parameters.put("type", type);
        String sign = MD5Utils.createSign(parameters);
        initdata(time, sign);
    }

    private void initdata(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.news_list)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("page", page + "")
                .addParams("type", type)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        ptrScrollView.onRefreshComplete();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG新闻列表/公告"+ response.toString());
                        dialog.dismiss();
                        ptrScrollView.onRefreshComplete();
                        postReponse(response);
                    }

                    public void postReponse(String json) {

                        NoticeBean enterBean = new Gson().fromJson(json, new TypeToken<NoticeBean>() {
                        }.getType());
                        int a = enterBean.getCode();
                        if (a == 1011) {
                            NoticeBean<List<NoticeBean.DataBean>> enter = new Gson().fromJson(json, new TypeToken<NoticeBean<List<NoticeBean.DataBean>>>() {
                            }.getType());

                            if (page == 1) {
                                list.clear();
                                //商品
                                list = enter.getData();
                            }
                            if (page > 1) {
                                list.addAll(enter.getData());
                            }
                            if (page == 1 && list.isEmpty()) {
                                rlGone.setVisibility(View.VISIBLE);
                                ptrScrollView.setVisibility(View.GONE);
                                return;
                            }
                            //商品
                            listdapter adapte = new listdapter();
                            rv_recyclerView.setAdapter(adapte);
                            adapte.notifyDataSetChanged();
                        } else if (a == 1004) {
                            Intent intent = new Intent(getBaseContext(), LoginActivity.class);
                            startActivity(intent);

                        } else {
                            showTips(enterBean.getMsg());
                        }


                    }
                });
    }

    public class listdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.notice_adapter_item, parent, false);
            return new MYholder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            MYholder holder1 = (MYholder) holder;
            holder1.setdata(position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        private class MYholder extends RecyclerView.ViewHolder {

            private final TextView tv_acction_name, tv_acction_time;

            public MYholder(View view) {
                super(view);
                tv_acction_name = view.findViewById(R.id.tv_acction_name);
                tv_acction_time = view.findViewById(R.id.tv_acction_time);
            }

            public void setdata(final int position) {
                tv_acction_name.setText(list.get(position).getTitle());
                tv_acction_time.setText(list.get(position).getCreate_time());
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(NoticeActivity.this, WebActivity.class);
                        intent.putExtra("url",list.get(position).getPath());
                        intent.putExtra("title",list.get(position).getTitle());
                        startActivity(intent);
                    }
                });

            }
        }
    }
}
